<template>
  <div class="mt-6"
    ><a-button type="primary" class="ml-2" @click="addAuthHandle">添加</a-button></div
  >
  <BasicTable @register="registerTable">
    <template #bodyCell="{ column, record }">
      <template v-if="column.key === 'action'">
        <TableAction
          :actions="[
            {
              icon: 'clarity:note-edit-line',
              label: '编辑',
              onClick: handleEdit.bind(null, record),
            },
            {
              icon: 'ant-design:delete-outlined',
              label: '删除',
              color: 'error',
              popConfirm: {
                title: '是否确认删除',
                placement: 'left',
                confirm: handleDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </template>
  </BasicTable>
  <ActionAuthModal @register="registerModal" @success="handleSuccess" />
</template>

<script lang="ts">
  import { defineComponent, watch, onMounted } from 'vue';
  import { actionAuths } from './menu.data';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useModal } from '/@/components/Modal';
  import ActionAuthModal from './ActionAuthModal.vue';
  // import { delUsinghandledefAuthApi } from '/@/api/admin/auth';

  export default defineComponent({
    // props: ['data', 'columns'],
    // emits: ['update:data', 'add', 'edit', 'del', 'delMutil', 'edit-mutil'],
    // setup(props, { emit }) {
    //   const { createMessage } = useMessage();
    //   const rawData = computed({
    //     get() {
    //       return props.data || [];
    //     },
    //     set(v) {
    //       return emit('update:data', v);
    //     },
    //   });

    name: 'ActionAuth',
    components: { BasicTable, TableAction, ActionAuthModal },
    props: ['data', 'moduleId'],
    emits: ['update:data', 'success'],
    setup(props, { emit }) {
      const { createMessage } = useMessage();
      const [registerModal, { openModal }] = useModal();

      const [registerTable, { setTableData }] = useTable({
        columns: actionAuths,
        pagination: false,
        striped: false,
        bordered: true,
        showIndexColumn: false,
        canResize: false,
        actionColumn: {
          width: 200,
          title: '操作',
          dataIndex: 'action',
          fixed: undefined,
        },
      });
      onMounted(() => {
        setTableData(props.data);
      });
      watch(
        () => props.data,
        (v) => {
          setTableData(v);
        },
      );

      function handleEdit(record: Recordable) {
        openModal(true, {
          record,
          isUpdate: true,
        });
      }

      async function handleDelete(record: Recordable) {
        const { id } = record;
        let ind = props.data.findIndex((e) => e.id === id);
        let newData = JSON.parse(JSON.stringify(props.data || []));
        newData.splice(ind, 1);
        emit('update:data', newData);
        // await delUsinghandledefAuthApi(handleId);
        createMessage.success('操作成功');
        // handleSuccess();
      }

      function addAuthHandle() {
        openModal(true, {
          moduleId: props.moduleId,
          idupdate: false,
        });
      }

      function handleSuccess(param, isUpdate) {
        let newData = JSON.parse(JSON.stringify(props.data || []));
        if (isUpdate.value) {
          let ind = props.data.findIndex((e) => e.id === param.id);
          newData[ind] = param;
        } else {
          param.id = `${newData.length + 1}`;
          newData.push(param);
        }
        // return;
        console.log('当前最新参数：', newData);
        emit('update:data', newData);
        emit('success', param, isUpdate);
      }
      return {
        registerTable,
        handleEdit,
        handleDelete,
        addAuthHandle,
        registerModal,
        handleSuccess,
      };
    },
  });
</script>
<style lang="less" scoped></style>
